#!/bin/sh

acpi=${1:-unknown}
full=${2:-nothing}

function wifi_off() {
 /sbin/rmmod ath_pci
 /sbin/rmmod ath_rate_sample
# /sbin/rmmod ath_hal

 /sbin/rmmod wlan_scan_sta
 /sbin/rmmod wlan_tkip
 /sbin/rmmod wlan_wep
 /sbin/rmmod wlan

 echo 0 > /sys/devices/platform/eeepc/wlan
}

function wifi_on() {
# /sbin/modprobe ath_rate_sample
 /sbin/modprobe ath_pci

# /sbin/modprobe wlan
# /sbin/modprobe wlan_wep
# /sbin/modprobe wlan_scan_sta

 echo 1 > /sys/devices/platform/eeepc/wlan
}

#echo `date`" '${acpi}' '${full}' - `id`" >> /etc/acpi.debug
#su eeedora -c "DISPLAY=:0 notify-send -i appointment -u critical \"$1\" \"$2\""
#exit

note='unknown'

if [ "${acpi}" == "wifi" ]; then
# ath=`/sbin/lsmod | grep ath_pci`
	PWR=$(cat /sys/devices/platform/eeepc/wlan)
	if [[ "$PWR" = "0" ]]; then  # If it's off already
		echo 1 > /sys/devices/platform/eeepc/wlan
 	rmmod pciehp
	fi

 if [ "off${ath}" == "off" ]; then 
  note='wifi-on'
  wifi_on
 else
  note='wifi-off'
  wifi_off
 fi
fi

if [ "${acpi}" == "zzz" ]; then
 wifi_off
 pm-suspend
 wifi_on
 note='resume'
fi

if [ "${acpi}" == "lid" ]; then
 wifi_off
 pm-suspend
 wifi_on
 note='resume'
fi

if [ "${acpi}" == "power" ]; then
# /sbin/shutdown -h now
# note='shutdown'
 wifi_off
 DISPLAY=:0 xfce4-panel -q
 wifi_on
 note=''
fi

# http://prouchon.free.fr/Linux/Edgy.html

if [ "${acpi}" == "mute" ]; then
 #o=`aumix -q | egrep 'vol 0, ?0'`
 o=`amixer get iSpeaker | egrep ' Playback \[on\]'`
 if [ -z "$o" ]; then
  amixer set iSpeaker unmute > /dev/null
  note='vol-unmute'
 else
  amixer set iSpeaker mute > /dev/null
  note='vol-mute'
 fi
fi

if [ "${acpi}" == "voldown" ]; then
 #/usr/bin/aumix -v-2
 amixer set LineOut 10%- > /dev/null
 note='vol-down'
fi

if [ "${acpi}" == "volup" ]; then
 #/usr/bin/aumix -v+2
 amixer set LineOut 10%+ > /dev/null
 note='vol-up'
fi

# http://wiki.zenwalk.org/index.php?title=Using_Asus_special_keys
# http://padre.web.elte.hu/linux_on_asus_l4l.html
if [ "${acpi}" == "vga" ]; then
# echo `echo ${full} | sed 's/.*0*6\(.\).*/\1/'` >/proc/acpi/asus/disp
 o=`DISPLAY=:0 xrandr | egrep 'VGA disconnected'`
 note='vga-disconnected'
 if [ -z "$o" ]; then # Not disconnected
  o=`DISPLAY=:0 xrandr | egrep '1280x1024.*\*'`
  if [ -z "$o" ]; then # not on yet
   DISPLAY=:0 xrandr --output VGA --mode 1280x1024
   note='vga-on'
  else
   DISPLAY=:0 xrandr --output VGA --off
   note='vga-off'
  fi 
 fi
fi

if [ "${acpi}" == "apps" ]; then
 # /etc/acpi/actions/apps
 /home/eeedora/.hotkey-app
 note=''
fi



if [ "empty${note}" != "empty" ]; then 
 su eeedora -c "DISPLAY=:0 /usr/bin/python /etc/acpi/actions/eee.py \"${note}\" \"${full}\""
# su - root -c "su eeedora -c \"/usr/bin/python /etc/acpi/actions/eee.py \"${note}\" \"${full}\"\""
fi

exit 0

#/usr/bin/python /etc/acpi/actions/eee.py "$1" "$2"
